<?php

//php5.6+

function sum(...$num)
{
    $acc = 0;
    foreach ($num as $n) {
        $acc += $n;
    }
    return $acc;
}

echo sum(...[1, 2, 3]);

//可变函数
function foo()
{
    echo "In foo() <br>";
}

$func = 'foo';
$func();

class Foo
{
    public function variable()
    {
        $name = "Bar";
        $this->$name();
    }
}

$foo = new Foo();
$funcname = 'variable';
$foo->$funcname();

class Cart
{
    const PRICE_BUTTER = 1.0;
    const PRICE_MILK = 3.99;
    const PRICE_EGGS = 5.99;
    protected $products = array();

    public function add($product, $quantity)
    {
        $this->$product[$product] = $quantity;
    }

    public function getQuantity($product)
    {
        return isset($this->product[$product]) ? $this->$product[$product] : false;
    }

    public function getTotal($tax)
    {
        $total = 0.00;
        $callback = function ($quantity, $product) use ($tax, &$total) {
            $pricePerItem = constant(__CLASS__ . "::PRICE_" . strtoupper($product));
            $total += ($pricePerItem * $quantity) * ($tax + 1.0);
        };
        array_walk($this->$product, $callback);
        return round($total, 2);
    }
}

$my_car = new Cart;

//往购物车里添加条目
$my_car . add('butter', 1);
$my_car . add('milk', 3);
$my_car . add('eggs', 6);

print $my_car->getTotal(0.05);